/**
 * 通用自定义表单处理
 * Copyright (c) 2020 osfw
 */

(function ($) {
    // 表单封装处理
    var myform = {
        // 表单重置
        reset: function (formId, tableId) {
            table.set(tableId);
            var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
            $("#" + currentId)[0].reset();
            if ($(".form-control").length > 0) {
                $(".form-control").select2();
            }
            var input = document.getElementsByTagName('input');
            for (var i = 0; i < input.length; i++) {
                if (input[i].classList.contains('noClear') == true) {
                } else if(input[i].name == 'searchOrgId'){
                    input[i].value = deptRootId;
                } else {
                    //清空隐藏值
                    input[i].value = "";
                }
            }
            // 表单重置回调函数
            if (typeof formResetCallBlack == 'function') {
                formResetCallBlack();
            }
            if (table.options.type == table_type.bootstrapTable) {
                if ($.common.isEmpty(tableId)) {
                    $("#" + table.options.id).bootstrapTable('refresh');
                } else {
                    $("#" + tableId).bootstrapTable('refresh');
                }
            } else if (table.options.type == table_type.bootstrapTreeTable) {
                if ($.common.isEmpty(tableId)) {
                    $("#" + table.options.id).bootstrapTreeTable('refresh', []);
                } else {
                    $("#" + tableId).bootstrapTreeTable('refresh', []);
                }
            }
            $("#tree #tree_1_ul li a").removeClass("curSelectedNode");
        },
        // 表单重置
        resetForCards: function (formId) {
            var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
            $("#" + currentId)[0].reset();
            if ($(".form-control").length > 0) {
                $(".form-control").select2();
            }
            var input = document.getElementsByTagName('input');
            for (var i = 0; i < input.length; i++) {
                if (input[i].classList.contains('noClear') == true) {
                } else if(input[i].name == 'searchOrgId'){
                    input[i].value = deptRootId;
                } else {
                    //清空隐藏值
                    input[i].value = "";
                }
            }
            // 表单重置回调函数
            if (typeof formResetCallBlack == 'function') {
                formResetCallBlack();
            }
            $.form.search();
        },
        // 表单重置
        resetForCards: function (formId) {
            var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
            $("#" + currentId)[0].reset();
            if ($(".form-control").length > 0) {
                $(".form-control").select2();
            }
            var input = document.getElementsByTagName('input');
            for (var i = 0; i < input.length; i++) {
                if (input[i].classList.contains('noClear') == true) {
                } else if(input[i].name == 'searchOrgId'){
                    input[i].value = deptRootId;
                } else {
                    //清空隐藏值
                    input[i].value = "";
                }
            }
            // 表单重置回调函数
            if (typeof formResetCallBlack == 'function') {
                formResetCallBlack();
            }
            $.form.search();
        },
        // 表单重置
        resetForTasks: function (formId) {
            var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
            $("#" + currentId)[0].reset();
            if ($(".form-control").length > 0) {
                $(".form-control").select2();
            }
            var input = document.getElementsByTagName('input');
            for (var i = 0; i < input.length; i++) {
                if (input[i].classList.contains('noClear') == true) {
                } else if(input[i].name == 'searchOrgId'){
                    input[i].value = deptRootId;
                } else {
                    //清空隐藏值
                    input[i].value = "";
                }
            }
            // 表单重置回调函数
            if (typeof formResetCallBlack == 'function') {
                formResetCallBlack();
            }
            $.form.search();
        },
        // 表单搜索
        search: function () {
            // 表单搜索回调函数
            if (typeof formSearchCallBlack == 'function') {
                formSearchCallBlack();
            }
        },
        // 详情页无法编辑
        disabledForm: function (formId) {
            $("form[id='"+ formId +"'] :input").attr("readonly", true);
            $("form[id='"+ formId +"'] :text").attr('readonly', true);
            $("form[id='"+ formId +"'] textarea").attr("readonly", true);
            $("form[id='"+ formId +"'] select").attr("disabled", true);
            $("form[id='"+ formId +"'] :radio").attr("disabled", true);
            $("form[id='"+ formId +"'] :checkbox").attr("disabled", true);
        }
    };

    // 合并至目标对象
    $.extend($.form, myform);
})(jQuery);
